//
//  SxV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/22.
//

import Foundation

class SxV: UIView {
    
    var bgView: UIView!
    
    var qb: UILabel!
    var wzc: UILabel!
    var yzc: UILabel!
    
    var state: [Int] = []
    
    var titles: [String]!
    var sxdict: [String: [String]]!
    var sarray: [String: [UILabel]]!
    
    var callBack: ((SxV?, String, [Int]) -> Void)!
    
    func showIn(_ SV: UIView, _ titles: [String], _ sxdict: [String: [String]], _ selectState: [Int], _ callBack: @escaping (SxV?, String, [Int]) -> Void) -> Void {
        self.clipsToBounds = true
        self.state = selectState
        self.callBack = callBack
        SV.addSubview(self)
        lgf_FillSuperview()
        
        self.titles = titles
        self.sxdict = sxdict
        sarray = [:]
        
        config()
        
        bgView.transform = CGAffineTransform.init(translationX: UIDevice.lgf_ScreenW * 0.75, y: 0)
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.3) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() {
        bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.top.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.75)
        }
        
        let label1 = UILabel.init()
        label1.text = "筛选"
        label1.font = UIFont.boldSystemFont(ofSize: 16)
        label1.textColor = UIColor.init(lgf_HexString: "222222")
        bgView.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15 + UIDevice.lgf_NavBarH() - 44)
            make.left.equalTo(8)
            make.height.equalTo(16)
        }
        
        
        for (fi, title) in titles.enumerated() {
            state.append(0)
            let label2 = UILabel.init()
            label2.text = title
            label2.font = UIFont.systemFont(ofSize: 15)
            label2.textColor = UIColor.init(lgf_HexString: "8D969D")
            bgView.addSubview(label2)
            label2.snp.makeConstraints { (make) in
                make.top.equalTo(bgView.subviews[bgView.subviews.count - 2].snp.bottom).offset(15)
                make.left.equalTo(label1)
                make.height.equalTo(15)
            }
            let sxarr = sxdict[title] ?? []
            var arr: [UILabel] = []
            var ni = 0
            for (i, v) in sxarr.enumerated() {
                ni += 1
                if i % 3 == 0 {
                    ni = 0
                }
                let qb = UILabel.init()
                qb.isUserInteractionEnabled = true
                qb.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.cgColor
                qb.layer.borderWidth = 0.0
                qb.layer.cornerRadius = 18
                qb.clipsToBounds = true
                qb.text = v
                qb.textColor = UIColor.init(lgf_HexString: "6E97B8")
                qb.backgroundColor = UIColor.init(lgf_HexString: "EDF1F4")
                qb.font = UIFont.systemFont(ofSize: 13)
                qb.textAlignment = .center
                if self.state[fi] == i + 1 {
                    labelSelect(qb)
                } else {
                    labelUnSelect(qb)
                }
                qb.lgf_AddTap(target: self, action: #selector(s_zt(_:)))
                bgView.addSubview(qb)
                qb.snp.makeConstraints { (make) in
                    make.left.equalTo(label2.snp.left).offset(ni * (Int(UIDevice.lgf_ScreenW * 0.75) / 3))
                    make.top.equalTo(label2.snp.bottom).offset(12 + floorf(Float(i) / 3.0) * 48)
                    make.height.equalTo(36)
                    make.width.equalTo((UIDevice.lgf_ScreenW * 0.75 - 40) / 3)
                }
                arr.append(qb)
            }
            sarray[title] = arr
        }
        
        
        let cz = UILabel.init()
        cz.isUserInteractionEnabled = true
        cz.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.cgColor
        cz.layer.borderWidth = 1.0
        cz.layer.cornerRadius = 4
        cz.clipsToBounds = true
        cz.text = "重置"
        cz.textColor = UIColor.init(lgf_HexString: "0F88F2")
        cz.font = UIFont.systemFont(ofSize: 13)
        cz.textAlignment = .center
        cz.lgf_AddTap(target: self, action: #selector(s_cz(_:)))
        bgView.addSubview(cz)
        cz.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(35)
            make.top.equalTo(bgView.subviews[bgView.subviews.count - 2].snp.bottom).offset(60)
            make.height.equalTo(40)
            make.width.equalTo((UIDevice.lgf_ScreenW * 0.75 - 90) / 2)
        }
        
        let qd = UILabel.init()
        qd.isUserInteractionEnabled = true
        qd.layer.cornerRadius = 4
        qd.clipsToBounds = true
        qd.text = "确定"
        qd.textColor = UIColor.white
        qd.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
        qd.font = UIFont.systemFont(ofSize: 13)
        qd.textAlignment = .center
        qd.lgf_AddTap(target: self, action: #selector(s_qd(_:)))
        bgView.addSubview(qd)
        qd.snp.makeConstraints { (make) in
            make.left.equalTo(cz.snp.right).offset(20)
            make.centerY.equalTo(cz)
            make.height.equalTo(40)
            make.width.equalTo((UIDevice.lgf_ScreenW * 0.75 - 90) / 2)
        }
        
    }
    
    @objc func s_zt(_ sender: UITapGestureRecognizer) {
        if let label = sender.view as? UILabel {
            for (i, title) in titles.enumerated() {
                let arr: [UILabel] = sarray[title] ?? []
                if arr.contains(label) {
                    arr.forEach { (l) in
                        labelUnSelect(l)
                    }
                    labelSelect(label)
                    let st = arr.lgf_IndexOfObject(label) + 1
                    state.remove(at: i)
                    state.insert(st, at: i)
                }
            }
        }
    }
    func labelSelect(_ label: UILabel) {
        label.textColor = UIColor.init(lgf_HexString: "0F88F2")
        label.backgroundColor = UIColor.init(lgf_HexString: "D4ECFF")
        label.layer.borderWidth = 1.0
        label.lgf_ViewName = "select"
    }
    
    func labelUnSelect(_ label: UILabel) {
        label.textColor = UIColor.init(lgf_HexString: "6E97B8")
        label.backgroundColor = UIColor.init(lgf_HexString: "EDF1F4")
        label.layer.borderWidth = 0.0
        label.lgf_ViewName = "unselect"
    }
    
    @objc func s_cz(_ sender: UIButton) {
        remove()
        debugPrint("筛选重置")
        state.removeAll()
        titles.forEach { (title) in
            state.append(0)
        }
        callBack?(self, self.lgf_ViewName ?? "", state)
    }
    
    @objc func s_qd(_ sender: UIButton) {
        remove()
        debugPrint("筛选:" + state.description)
        callBack?(self, self.lgf_ViewName ?? "", state)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.45, animations: {
            self.bgView.transform = CGAffineTransform.init(translationX: UIDevice.lgf_ScreenW * 0.75, y: 0)
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
    
}
